package com.urfresh.sp.flume.configure;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.text.SimpleDateFormat;

@SpringBootApplication
@ComponentScan("com.urfresh.sp.flume")
@EnableSwagger2
public class Application extends SpringBootServletInitializer {

//	@Value("${flume.hostname}")
//	String flumeHostname;
//	@Value("${flume.port}")
//	int flumePort;

	@Value("${mongo.db.url1}")
	String mongoUrl1;
	@Value("${mongo.db.url2}")
	String mongoUrl2;
	@Value("${mongo.db.url3}")
	String mongoUrl3;
	@Value("${mongo.db.port1}")
	int mongoPort1;
	@Value("${mongo.db.port2}")
	int mongoPort2;
	@Value("${mongo.db.port3}")
	int mongoPort3;
	@Value("${mongo.db.name}")
	String mongoDbName;
	@Value("${mongo.db.collection}")
	String mongoCollection;
	@Value("${mongo.auth.name}")
	String mongoAuthName;
	@Value("${mongo.auth.password}")
	String mongoAuthPassword;
	@Value("${mongo.db.replicaSet}")
	String replicaSet;
	@Value("${mongo.db.readPreference}")
	String readPreference;

	@Value("${product}")
	boolean product;
	@Value("${mongo.db.url.test}")
	String mongoUrlTest;
	@Value("${mongo.db.port.test}")
	int mongoPortTest;

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public SimpleDateFormat simpleDateFormat(){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat;
	}

	@Bean
	public WebMvcConfigurer corsConfigurer() {
		return new WebMvcConfigurerAdapter() {
			@Override
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/**")
						.allowedOrigins("*")
						.allowedMethods("POST");
			}
		};
	}
}
